package com.hmall.order.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * publish-return
 * RabbitTemplate中只能有一个ReturnCallback
 */
@Configuration
@Slf4j
public class CommonConfig implements ApplicationContextAware {

    /**
     * setApplicationContext 可以获取spring的上下文（spring容器）
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //通过spring容器得到RabbitTemplate
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        //得到RabbitTemplate设置ReturnCallback回调
        //匿名内部类 -- 简写 lambda(alt+enter)
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            //存入日志
            //业务相应的错误提示-系统繁忙
            //相应警告-告警-通知运维人员进行解决
            log.debug("消息投递到队列失败了，消息的内容{}，应答码{},应答的内容{}，交换机{},路由key{}",message, replyCode, replyText, exchange, routingKey);
        });
    }
}
